//
//  CZStatusForwardCell.swift
//  czbkiosweibo5
//
//  Created by zhangping on 15/10/19.
//  Copyright © 2015年 zhangping. All rights reserved.
//

import UIKit

/// 转发微博cell
class CZStatusForwardCell: CZStatusCell {
    
    /*
        覆盖父类的属性:
            1.需要添加 override关键字
            2.需要添加属性监视器
            3.先调用父类属性监视器,在调用子类属性监视器
    */
    /// 微博模型
    override var status: CZStatus? {
        didSet {
//            print("子类属性: \(status!.id)")
            
            // 设置转发微博内容
            let name = status?.retweeted_status?.user?.name ?? "名称为空"
            let text = status?.retweeted_status?.text ?? "转发微博内容为空"
            forwardLabel.text = "@\(name):\(text)"
        }
    }
    
    /// 重写父控件prepareUI
    override func prepareUI() {
        // 先调用父控件的方法
        super.prepareUI()
        
        // 测试数据
//        forwardLabel.text = "转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博"
        
        // 添加子控件
        contentView.insertSubview(backButton, belowSubview: pictureView)
        contentView.addSubview(forwardLabel)
        
        // 添加约束
        // 被转发微博背景,垂直方向:在contentLabel左下,在bottomView右上
        backButton.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: nil, offset: CGPoint(x: -statusCellMargin, y: statusCellMargin))
        backButton.ff_AlignVertical(type: ff_AlignType.TopRight, referView: bottomView, size: nil)

        // 被转发微博内容
        forwardLabel.ff_AlignInner(type: ff_AlignType.TopLeft, referView: backButton, size: nil, offset: CGPoint(x: statusCellMargin, y: statusCellMargin))
        
        // 配图视图
        // 在内容标签的左下位置,距离右边为8,宽高为0, 0
        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: forwardLabel, size: CGSize(width: 0, height: 0), offset: CGPoint(x: 0, y: statusCellMargin))
        // 获取配图视图的宽度约束
        pictureViewWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
        
        // 获取配图视图的高度约束
        pictureViewHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
    }

    // MARK: - 懒加载控件
    /// 被转发微博内容
    private lazy var forwardLabel: UILabel = {
        // 创建label
        let label = UILabel()
        
        // 设置显示多行
        label.numberOfLines = 0

        // 设置折行文本宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * self.statusCellMargin
        
        return label
    }()
    
    /// 被转发微博背景
    private lazy var backButton: UIButton = {
        let button = UIButton()
        
        // 按钮背景
        button.backgroundColor = UIColor(white: 0.85, alpha: 1)
        
        return button
    }()
}
